/**
 * 
 */
package jnzb.control;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author AA1
 * 
 */
public class RetryTimer extends TimerTask {

	private Timer timer = null;
	private int timeout = 0;
	private Controller controller = null;
	private boolean timerScheduled = false;

	/**
	 * Create a new instance of the retry timer that times out as defined in the
	 * configuration file
	 */
	public RetryTimer(Controller controller) {
		this.timeout = Configuration.getInstance().getAutoRetryValue();
		this.controller = controller;
		this.timer = new Timer(false);
	}

	/**
	 * Cancels the auto retry timer
	 */
	public void cancelTimer() {
		if (timer != null && this.timerScheduled) {
			this.timer.cancel();
		}
		this.timerScheduled = false;
	}

	/**
	 * Indicates if the retry timer is currently scheduled
	 * 
	 * @return
	 */
	public boolean isTimerScheduled() {
		return this.timerScheduled;
	}

	/**
	 * Schedule the timer
	 */
	public void scheduleTimer() {
		cancelTimer();
		this.timer.schedule(this, this.timeout * 1000);
		this.timerScheduled = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.TimerTask#run()
	 */
	@Override
	public void run() {
		this.controller.autoRetryEvent();
		this.timerScheduled = false;
	}

}
